home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 August: Tool Chest / Apple_Developer_CD_Series_August_1997_Tool_Chest.iso / Sample Code / Overview / CPlusTESample / Application.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-18  |  5.5 KB  |  176 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------------------
  2.  
  3.     Program:    CPlusTESample 2.0
  4.     File:        Application.h
  5.     Uses:       AppLib.h
  6.  
  7.     by Andrew Shebanow
  8.     of Apple Macintosh Developer Technical Support
  9.  
  10.     Copyright © 1989-1990 Apple Computer, Inc.
  11.     All rights reserved.
  12.  
  13. ------------------------------------------------------------------------------------------*/
  14.  
  15. #ifndef __APPLICATION__
  16. #define __APPLICATION__
  17.  
  18. // Include necessary interface files
  19. #ifndef __STRING__
  20. #include <String.h>
  21. #endif
  22.  
  23. #ifndef __TYPES__
  24. #include <Types.h>
  25. #endif
  26. #ifndef __MENUS__
  27. #include <Menus.h>
  28. #endif
  29. #ifndef __DEVICES__
  30. #include <Devices.h>
  31. #endif
  32. #ifndef __EVENTS__
  33. #include <Events.h>
  34. #endif
  35. #ifndef __OSUTILS__
  36. #include <OSUtils.h>
  37. #endif
  38.  
  39. // we need resource ids
  40. #ifndef __APPLIB__
  41. #include "AppLib.h"
  42. #endif
  43.  
  44. // we need the exceptions code
  45. #ifndef __EXCEPTIONS__
  46. #include "Exceptions.h"
  47. #endif
  48.  
  49. class TDocument;
  50. class TDocumentList;
  51.  
  52. // YNCResult is a type used to indicate a result from
  53. // a user operation. YNC means yes/no/cancel, but yes
  54. // and no can also have synonyms like Save and Discard.
  55. enum YNCResult { yesResult, noResult, cancelResult };
  56.  
  57. /*
  58.     TApplication:
  59.  
  60.     This is our class which implements a basic Macintosh style program,
  61.     including a MultiFinder-aware event loop.
  62. */
  63.  
  64. // we derive from handle object to prevent fragmentation
  65. class TApplication : public HandleObject {
  66. private:
  67.     static OSType fCreator;
  68.  
  69. public:
  70.     // Our constructor
  71.     TApplication(OSType creator);
  72.  
  73.     // process finder arguments
  74.     virtual void    ProcessArgs();
  75.  
  76.     // Call this routine to start event loop running
  77.     // It will not return until the user quits the application (via a call
  78.     // to DoQuit, below)
  79.     virtual void    EventLoop();
  80.  
  81.     // Utility routines you can use
  82.     static Boolean            TrapAvailable(short tNumber,TrapType tType);
  83.         // Is trap implemented???
  84.     inline TDocumentList*    DocList() { return fDocList; };
  85.     static OSType            GetCreator() { return fCreator; };
  86.     static void                WDToDirID(short wdRefNum, short& vRefNum, long& dirID);
  87.         // utility routine to convert a working directory to a vRefNum/dirID pair
  88.  
  89. protected:
  90.     virtual long    StackNeeded() { return 0; };
  91.         // Returns total stack space required in bytes.
  92.         // Returns 0 by default, which tells the initialization code
  93.         // to use the default stack size.
  94.     virtual long    HeapNeeded() { return 0; };
  95.         // Returns total heap space required in bytes.
  96.         // Returns 0 by default, which tells the initialization code
  97.         // to use whatever heap size is given.
  98.  
  99.     // called by EventLoop to process an individual event
  100.     virtual void    ProcessEvent();
  101.  
  102.     // Loop control methods you may need to override
  103.     virtual void    SetUp() {};                // Run before event loop starts
  104.     virtual void    DoIdle() {};            // idle time handler (blink caret, background tasks)
  105.     virtual void    AdjustMenus() {};        // menu updater routine
  106.  
  107.     // event handlers you shouldn't need to override in a typical application
  108.     virtual void    DoOSEvent();
  109.         // Calls DoSuspend, DoResume and DoIdle as apropos
  110.     virtual void    DoMouseDown();
  111.         // Calls DoContent, DoGrow, DoZoom, etc
  112.     virtual void    DoKeyDown();
  113.         // also called for autokey events
  114.     virtual void    DoActivateEvt();
  115.         // handles setup, and calls DoActivate (below)
  116.     virtual void    DoUpdateEvt();
  117.         // handles setup, and calls DoUpdate (below)
  118.     virtual void    DoMouseInSysWindow() { SystemClick(&fTheEvent, fWhichWindow); };
  119.     virtual void    DoDrag();
  120.     virtual void    DoGoAway();
  121.         // handles setup, and calls TDocument::DoClose
  122.     virtual void    DoQuit(Boolean askUser, YNCResult defaultResult);
  123.  
  124.     // handlers you MUST override for functionality:
  125.     virtual void    AdjustCursor() = 0;
  126.         // cursor adjust routine, should setup mouseRgn
  127.     virtual void    DoMenuCommand(short menuID, short menuItem) = 0;
  128.     virtual void    DoNew() = 0;
  129.     virtual void    DoOpen() = 0;
  130.     virtual void    OpenADoc(short vRefNum, long dirID,
  131.                              StringPtr fName, OSType fType) = 0;
  132.  
  133.     // called by OSEvent (just calls DoActivate by default, so no clip conversion
  134.     // is done). If you want to convert clipboard, override these routines
  135.     virtual void    DoSuspend(Boolean doClipConvert);
  136.     virtual void    DoResume(Boolean doClipConvert);
  137.  
  138.     // If you have an app that needs to know about these, override them
  139.     virtual void    DoMouseUp() {};
  140.     virtual void    DoDiskEvt() {};
  141.  
  142.     // Utility routines you need to provide to do MultiFinder stuff
  143.     virtual unsigned long SleepVal() { return 0; };
  144.         // how long to sleep in WaitNextEvent
  145.  
  146.     // useful variables
  147.     Boolean            fHaveWaitNextEvent;        // true if we have WaitNextEvent trap
  148.     Boolean            fDone;                    // set to true when we are ready to quit
  149.     Boolean            fOnSystem7;                // running system 7 or better
  150.     EventRecord        fTheEvent;                // our event record
  151.     WindowPtr        fWhichWindow;            // currently active window
  152.     Boolean            fInBackground;            // true if our app is suspended
  153.     Boolean            fWantFrontClicks;        // true if we want front clicks
  154.     RgnHandle        fMouseRgn;                // mouse moved region (set it in your DoIdle)
  155.     TDocument*        fCurDoc;                // currently active document (if any)
  156.     TDocumentList*    fDocList;                // the list of documents
  157. };
  158.  
  159. // some other handy utility routines, not actually a part of application class
  160.  
  161. // display alert, using specified error STR# resource and error code as index
  162. void AlertUser(short errResID, short errCode);
  163. // call AlertUser to display error message, then quit...
  164. void BigBadError(short errResID, short errCode);
  165. Boolean LookupErrorString(short value, short resID, StringPtr str);
  166.  
  167. // useful state checking Booleans (mostly for System 7 compatibility)
  168. extern Boolean gHaveColorQD;
  169.  
  170. inline void CopyPString(StringPtr to, StringPtr from)
  171. {
  172.     memcpy(to,from,(size_t) (from[0] & 0x0ff) + 1);
  173. }
  174.  
  175. #endif
  176.